import {
	getStore,
	setStore,
} from '@/common/util';

import cityData from '@/common/city.data.js';
import {getFirstInfo} from '@/common/cityUtils';
import {addAddressItem,
getAddressItems,
updateAddressItem,
deleteAddressItem} from '@/api/ucenter/index'
const address = {
	state: {
		addresses: getStore({
		name: '$sc_address'
		})||[]
	},
	mutations: {
		loadAddresses: function(state, data) {
			state.addresses = data;
			setStore({
			name: '$sc_address',
			content: state.addresses,
			type: 'session'
			})
		},
		addAddress: function(state, data) {
			console.log("xxx" + JSON.stringify(state));
			
			state.addresses.push(data);
			setStore({
			name: '$sc_address',
			content: state.addresses,
			type: 'session'
			})
		},
		deleteAddress(state, index) {
			state.addresses.splice(index, 1);
			setStore({
			name: '$sc_address',
			content: state.addresses,
			type: 'session'
			})
		},
		updateAddress(state, data) {
			state.addresses.splice(data.index, 1);
			state.addresses.unshift(data.item);

			setStore({
				name: '$sc_address',
				content: state.addresses,
				type: 'session'
			})
		}
	},
	actions: {
		loadAddressAction: function({
			commit,
			state
		}, userId) {
			console.log("loadAddressAction:" + userId+ ";length:"+ state.addresses.length)
		
			return new Promise((resolve, reject) => {
					if(state.addresses.length > 0) {
						console.log(JSON.stringify(state.addresses))
						resolve();
						
					} else {
						getAddressItems({"partyId": userId}).then(res => {
							console.log(JSON.stringify(res))
							if(res.length > 0 ) {
								for(var i = 0; i < res.length; i++) {
									var fistAddress = getFirstInfo(cityData,res[i].stateProvinceGeoId, res[i].city,res[i].countyGeoId);
									var address = fistAddress.provice+fistAddress.city+(fistAddress.county!== undefined ? fistAddress.county: "");
									res[i].pickText = address;
									if(res[i].address1 !== undefined) {
										address = address + res[i].address1
									}
									if(res[i].address2 !== undefined) {
										res[i].address1 = res[i].address1 + res[i].address2
									}
									if(res[i].isDefault == "Y") {
										res[i].checked = true;
									}
									res[i].address = address + res[i].address1;
								
								console.log("loadAddresses finished")
								commit("loadAddresses",res)
							}
							}
							resolve();
						}).catch(error => {
								reject(error)
						});
					}
					
				
				
			})
		},
		updateAddressAction: function({
			commit,
			state
		}, cmmitItem) {
			console.log("updateAddressAction:"+ JSON.stringify(cmmitItem))

			return new Promise((resolve, reject) => {
				console.log("updateAddressAction begin:")
				if(cmmitItem.key == "checked") {
					cmmitItem.addressItem.isDefault = (cmmitItem.addressItem.checked == true ? "Y": "N");
				}
				console.log("updateAddressAction 1:")
				updateAddressItem(cmmitItem.addressItem).then(res => {
					console.log("updateAddressAction result:" + JSON.stringify(res))
					commit("updateAddress", {index:cmmitItem.index,
					item:cmmitItem.addressItem});
					console.log("updateAddress resolve......")
					resolve();
				}).catch(error => {
					reject(error)
				}) 
				console.log("updateAddressAction end:")
			});
		},
		addAddressAction: function({
			commit,
			state
		}, address) {
			return new Promise((resolve, reject) => {
				addAddressItem(address).then(res => {
					console.log(JSON.stringify(res));
					commit("addAddress", address)
					resolve();
				}).catch(error => {
					reject(error)
				}) 
			})
		},
		deleteAddressAction: function({
			commit,
			state
		}, address) {
			return new Promise((resolve, reject) => {
				deleteCartItem(address.contactListId).then(res => {
					console.log(JSON.stringify(res));
					commit("deleteAddress", cartItem.index)
					resolve();
				}).catch(error => {
					reject(error)
				}) 
			})
		},
	}
}


export default address